package com.haha.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

public class RateCalcActivity extends AppCompatActivity {

    private EditText input;
    private TextView title;
    private TextView show;
    private float rate;
//    String TAG = "rateCalc";
//    float rate = 0f;
//    EditText inp2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ratecalc);

        title = findViewById(R.id.title2);
        input = findViewById(R.id.input2);
        show = findViewById(R.id.show2);

        Intent intent = getIntent();
        String cname = intent.getStringExtra("cname_key");
        title.setText(cname);
        String cval = intent.getStringExtra("cval_key");
        show.setText(cval);
        rate = Float.parseFloat(cval);

        //参数获取及汇率计算
//        String title = getIntent().getStringExtra("title");
//        rate = getIntent().getFloatExtra("rate",0f);
//
//        Log.i(TAG, "onCreate: title = " + title);
//        Log.i(TAG, "onCreate: rate = " + rate);
//        ((TextView)findViewById(R.id.title2)).setText(title);
//        inp2 = (EditText)findViewById(R.id.input2);

        input.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String str = s.toString();
//                TextView show = (TextView) RateCalcActivity.this.findViewById(R.id.show2);
                if(s.length()>=0){
                    String result = String.valueOf(Float.parseFloat(str) * 100/rate);
                    show.setText(result);
//                    float val = Float.parseFloat(s.toString());
//                    show.setText(val + "RMB-->" + (100/rate*val));
                }else{
                    show.setText("");
                }
            }
        });
    }
}